home *** CD-ROM | disk | FTP | other *** search
- function $debug( msg )
- {
- var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
- .getService(Components.interfaces.nsIConsoleService);
-
- consoleService.logStringMessage( "[feedly][extension]" + msg );
- }
-
- var ExceptionUtils = function()
- {
- var that = {};
-
- that.formatError = function( task, e )
- {
- var errorMsg = "[error]failed to " + task + " because " + e.name + " -- " + e.message;
-
- if( e.fileName != null )
- errorMsg += " in file '" + e.fileName + "'"
-
- if( e.lineNumber != null )
- errorMsg += " at line number " + e.lineNumber;
-
- return errorMsg;
- }
- return that;
- }();
-
- var feedlyExBoot = function()
- {
- var that = {};
-
- //---------------------------------------------------------------------
- // Browser progress listener
- //---------------------------------------------------------------------
- var NS_BINDING_ABORTED = 0x804b0002;
- that.onStateChange = function( wp, request, state, result ){}
- that.onStatusChange = function ( wp, request, result, messages ){}
- that.onSecurityChange = function ( wp, request, state ){}
- that.onProgressChange = function( ){ }
-
- var boot = null;
-
- var lastErrorMsg = null;
-
- // DELEGATE CALLS TO TO THE EXTENSION
- that.onLocationChange = function ( wp, request, locationURI )
- {
- try
- {
- // I am not sure why, but this seems to happen sometimes.
- if( locationURI == null )
- return;
-
- // no pre-processing for the error page.
- if( locationURI.spec.indexOf( "chrome://feedly/content/boot/error.htm" ) == 0 )
- return;
-
- // If there is an error message, redirect the user to the UI displaying the error message.
- if( lastErrorMsg != null && locationURI.spec.indexOf( "http://www.feedly.com/home" ) == 0 )
- {
- request.cancel( NS_BINDING_ABORTED );
- wp.DOMWindow.document.location = "chrome://feedly/content/boot/error.htm?msg=" + encodeURIComponent( lastErrorMsg );
- return;
- }
-
- // no pre-processing for the splash page from this point on
- if( locationURI.spec.indexOf( "chrome://feedly/content/boot/splash.htm" ) == 0 )
- return;
-
- if( boot != null && boot.getBootError() != null )
- {
- if( locationURI.spec.indexOf( "http://www.feedly.com/home" ) == 0 )
- {
- request.cancel( NS_BINDING_ABORTED );
-
- // redirect browser
- var errorMsg = "extension loader can not look up boot component";
- if( boot != null )
- errorMsg = boot.getBootError();
-
- wp.DOMWindow.document.location = "chrome://feedly/content/boot/error.htm?msg=" + encodeURIComponent( errorMsg );
- }
- return;
- }
-
- // feedly application not loaded yet. Display the splash screen.
- if( that.extension == null )
- {
- if( locationURI.spec.indexOf( "http://www.feedly.com/home" ) == 0 )
- {
- // DBH $debug( "redirecting from " + locationURI.spec + " to splash screen" );
- request.cancel( NS_BINDING_ABORTED );
-
- // redirect browser
- wp.DOMWindow.document.location = "chrome://feedly/content/boot/splash.htm?" + encodeURIComponent( "Updating feedly. Please wait..." );
- }
- return;
- }
- else
- {
- that.extension.onLocationChange( wp, request, locationURI );
- }
- }
- catch( e )
- {
- $debug( "[boot][exception]" + e.name + " -- " + e.message + " -- " + e.fileName + " -- " + e.lineNumber );
- var msg = devhd.utils.ExceptionUtils.formatError( "route request", e );
- $debug( "[boot]" + msg );
- }
- }
-
- that.onImageClick = function()
- {
- if( that.extension != null )
- that.extension.onImageClick();
- }
-
- that.onCommand = function( e )
- {
- if( that.extension == null )
- loadPage( "chrome://feedly/content/boot/splash.htm?" + encodeURIComponent( "Updating feedly. Please wait..." ) );
- else
- that.extension.onCommand( e );
- }
-
- function loadPage( pageURI )
- {
- var tabBrowser = getBrowser();
-
- // navigate through list of existing tabs
- var tabs = tabBrowser.tabContainer.childNodes;
- for( var i = 0; i < tabs.length; i++ )
- {
- var aTab = tabs[ i ];
- var aBrowser = tabBrowser.getBrowserForTab( aTab );
- var currentLocationURI = new String( aBrowser.contentDocument.location );
-
- // per olivier's request, open feedly in existing empty page.
- if( currentLocationURI == "about:blank" )
- {
- aBrowser.contentDocument.location.href = pageURI;
- tabBrowser.selectedTab = aTab;
- return;
- }
-
- if( currentLocationURI.indexOf( "chrome://feedly/content" ) == 0 )
- {
- if( aBrowser.contentDocument.location != pageURI )
- aBrowser.contentDocument.location.href = pageURI;
-
- tabBrowser.selectedTab = aTab;
- return;
- }
- };
-
- // no pre-existing tabs. create a new one.
- var tab = tabBrowser.addTab( pageURI );
- var aBrowser = tabBrowser.getBrowserForTab( tab );
- tabBrowser.selectedTab = tab;
- return;
- };
-
- // event notifying us that the boot has finished to load the core in memory
- that.onCoreLoaded = function()
- {
- var phase = "";
- try
- {
- // Load JS libraries
- ///D $debug( "[extension loader] loading extension in memory..." );
-
- phase = "loading feedlyExtension.js in memory";
- var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
- .getService( Components.interfaces.mozIJSSubScriptLoader );
-
- jsLoader.loadSubScript( "chrome://feedly/content/app/extension/feedlyExtension.js" );
-
- // ask extension to load itself.
- ///D $debug( "[extension loader] asking extension to load..." );
- phase = "asking extension to load";
- feedlyExtension.onLoad();
-
- ///D $debug( "[extension loader] notifying the extension that the core is ready..." );
- phase = "notifying the extension that the core is ready.";
- feedlyExtension.onCoreLoaded( boot.lookupCore() );
-
- // assign the extension to the that only when everything is loaded and ready to make sure that
- // the extension is protected and no location change can propage from this place.
- that.extension = feedlyExtension;
- }
- catch( e )
- {
- that.extension = null;
- lastErrorMsg = " failed to load extension in phase: " + phase + " because: " + e.name + " -- " + e.message + " -- " + e.fileName + " -- " + e.lineNumber;
- $debug( "[extension-boot]" + lastErrorMsg );
- }
- }
-
- that.onBootError = function( errorMsg )
- {
- $debug( "[extension loader] boot error: " + errorMsg );
- lastErrorMsg = errorMsg;
- displayError();
- }
-
- function displayError()
- {
- var tabBrowser = getBrowser();
- var tabs = tabBrowser.tabContainer.childNodes;
- for( var i = 0; i < tabs.length; i++ )
- {
- var aTab = tabs[ i ];
- var aBrowser = tabBrowser.getBrowserForTab( aTab );
- var aLoc = new String( aBrowser.contentDocument.location );
- if( aLoc.indexOf( "chrome://feedly/content" ) == 0 )
- {
- aBrowser.contentDocument.location = "chrome://feedly/content/boot/error.htm?msg=" + encodeURIComponent( lastErrorMsg );
- }
- }
- }
-
- function handleOnPageShow()
- {
- if( that.extension != null )
- that.extension.onPageShown();
- }
-
- that.onLoad = function()
- {
- try
- {
- // monitor the browser
- getBrowser().addProgressListener( that );
- getBrowser().addEventListener( "pageshow", handleOnPageShow, false );
-
- boot = Components.classes["@devhd.com/feedly-boot;1"]
- .getService( Components.interfaces.nsIFeedlyBoot )
- .wrappedJSObject;
-
- if( boot == null )
- {
- $debug( "[onload] skiping load sequence because could not link to core feedddo boot component" );
- return;
- }
-
- if( boot.getBootError() != null )
- {
- $debug( "[onload] skiping load sequence because boot has faulted:" + boot.getBootError() );
- lastErrorMsg = boot.getBootError();
- return;
- }
-
- ///D $debug( "[extension][boot] linked to core feedly boot" );
- boot.registerObserver( that );
-
- // ask been if the core has been loaded. If it has, we know that we need to load
- if( boot.isCoreLoaded() )
- that.onCoreLoaded( );
- }
- catch( e )
- {
- lastErrorMsg = "failed to load extension boot because:" + e.name + " -- " + e.message + " -- " + e.fileName + " -- " + e.lineNumber;
- $debug( "[extension][boot]" + lastErrorMsg );
- }
- };
-
- that.onUnload = function()
- {
- getBrowser().removeProgressListener( that );
- getBrowser().removeEventListener( "pageshow", handleOnPageShow, false );
-
- if( that.extension != null )
- that.extension.onUnload();
- that.extension = null;
-
- if( boot != null )
- boot.unregisterObserver( that );
- boot = null;
-
- that.initialized = false;
- };
-
- return that;
- }();
-
- function loadFeedlyExtensionBoot()
- {
- window.removeEventListener( "load", loadFeedlyExtensionBoot , false );
- feedlyExBoot.onLoad();
- ///D $debug( "[extension][boot] loaded" );
- }
-
- function unloadFeedlyExtensionBoot()
- {
- window.removeEventListener( "unload", unloadFeedlyExtensionBoot , false );
- feedlyExBoot.onUnload();
- feedlyExBoot = null;
- ///D $debug( "[extension][boot] unloaded" );
- }
-
-
- window.addEventListener( "load", loadFeedlyExtensionBoot, false );
- window.addEventListener( "unload", unloadFeedlyExtensionBoot, false );
-